export function generateUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        const r = Math.random() * 16 | 0
        const v = c === 'x' ? r : (r & 0x3 | 0x8)
        return v.toString(16)
    })
}

export function formatter(type, value) {
    if (type === 'year') {
        return `${value}年`;
    }
    if (type === 'month') {
        return `${value}月`;
    }
    if (type === 'day') {
        return `${value}日`;
    }
    if (type === 'hour') {
        return `${value}时`;
    }
    if (type === 'minute') {
        return `${value}分`;
    }
    return value;
}


/**
 * 格式化时间戳为 YYYY-MM-DD 或 YYYY-MM-DD HH:mm:ss
 * @param {number|string|Date} timestamp - 时间戳（毫秒）或 Date 实例
 * @param {boolean} [includeTime=true] - 是否包含时分秒，默认包含
 * @returns {string} 格式化后的日期字符串
 */
export function formatTimestamp(timestamp, includeTime = true) {
    const date = new Date(timestamp);
    if (isNaN(date.getTime())) return '';

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');

    const datePart = `${year}-${month}-${day}`;

    if (!includeTime) {
        return datePart;
    }

    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${datePart} ${hours}:${minutes}:${seconds}`;
}


export function formatDateTime(date) {
    const pad = (n) => (n < 10 ? '0' + n : n);
    const yyyy = date.getFullYear();
    const mm = pad(date.getMonth() + 1); // 月份从0开始
    const dd = pad(date.getDate());
    const hh = pad(date.getHours());
    const min = pad(date.getMinutes());
    const ss = pad(date.getSeconds());
    return `${yyyy}-${mm}-${dd} ${hh}:${min}:${ss}`;
}